﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using TextToHTMLExtensionInterfaces;

namespace TextToHTML
{
    /// <summary>
    /// Given a URI, return the html to display it.
    /// We only deal with simple pictures here!
    /// </summary>
    [Export(typeof(IRenderToHTML))]
    [ExportMetadata("type", typeof(Uri))]
    class RenderSimplePicture : IRenderToHTML
    {
        /// <summary>
        /// Is this a picture format we know how to deal with?
        /// </summary>
        /// <param name="objToRender"></param>
        /// <returns></returns>
        public bool CanRender(object objToRender)
        {
            Uri u = objToRender as Uri;
            if (u == null)
                return false;

            string t = u.AbsolutePath;
            int dot = t.LastIndexOf(".");
            if (dot < 0)
            {
                return false;
            }
            t = t.Substring(dot+1);

            return t == "jpeg"
                || t == "jpg"
                || t == "gif"
                || t == "png";
        }

        /// <summary>
        /// These ones are easy - they are just the URI's that we have sitting around! No Conversion Needed! :-)
        /// </summary>
        /// <param name="objToRender"></param>
        /// <returns></returns>
        public string Render(object objToRender)
        {
            Uri u = objToRender as Uri;
            return "<img src=\"" + u.AbsoluteUri + "\" />";
        }
    }
}
